<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:doc="livcos.org/ns/doc"
	xmlns:dg="livcos.org/ns/doc/diagram"
	xmlns:code="livcos.org/ns/dev/code"
	xmlns:cm="livcos.org/ns/codeml"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:f="livcos.org/ns/xslt/function"
	exclude-result-prefixes="doc dg f xs code cm">

	<xsl:import href="../../../style/base.xsl"/>
	
	<xsl:variable name="javaDocLink" select="f:entity('/localhost/data/livcos.org/config#doc-link_2', /)"/>
	<xsl:variable name="srcDocLink" select="f:entity('/localhost/data/livcos.org/config#doc-link_1', /)"/>
	
	<xsl:template match="code:walk">
		<div id="{@id}">
			<xsl:element name="h{count(ancestor::doc:chapter)+1}">
				<span class="inlineEdit" title="title">
					<xsl:apply-templates select="code:title/node()"/>
				</span>
			</xsl:element>
			<xsl:apply-templates select="* except code:*"/>
			<div class="src_path_list">
				<xsl:apply-templates select="code:call"/>
			</div>
		</div>
	</xsl:template>
	
	<xsl:template match="code:call">
		<xsl:variable name="m" select="f:entity(@ref, .)"/>
		<div id="{@id}" class="src_loc">
			<xsl:apply-templates select="$m">
				<xsl:with-param name="call" select="."/>
			</xsl:apply-templates>
			<xsl:if test="not($m)">
				<xsl:text>-- method </xsl:text>
				<xsl:value-of select="@ref"/>
				<xsl:text>--</xsl:text>
			</xsl:if>
			<xsl:choose>
				<xsl:when test="code:call">
					<div style="padding-left: 4px; border-left: dotted 1px grey;">
						<xsl:apply-templates select="* except code:*"/>
						<xsl:apply-templates select="code:call"/>
					</div>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="* except code:*"/>
				</xsl:otherwise>
			</xsl:choose>
		</div>
	</xsl:template>
	
	<xsl:template match="cm:method">
		<xsl:param name="call" as="element()"/>
		<xsl:variable name="_pts" select="cm:param/cm:type/@ref-name"/>
		<xsl:variable name="_uri" select="replace(../@id-name, '\.', '/')"/>		
		<xsl:variable name="javaDoc_signature" 
			select="string-join(for $p in $_pts return replace($p, '&lt;.*&gt;', ''), ', ')"/>
		<a href="{$javaDocLink/@url}{$_uri}.{$javaDocLink/@ext}#{@name}({$javaDoc_signature})"
			class="src_loc_title">
			<xsl:attribute name="title">
				<xsl:value-of select="../@id-name"/>
				<xsl:text>.</xsl:text>
				<xsl:value-of select="@name"/>
				<xsl:text>(</xsl:text>
				<xsl:for-each select="cm:param">
					<xsl:if test="position() > 1">, </xsl:if>
					<xsl:apply-templates select="cm:type"/>
					<xsl:text> </xsl:text>
					<xsl:value-of select="@name"/>
				</xsl:for-each>
				<xsl:text>)</xsl:text>
			</xsl:attribute>
			<xsl:value-of select="../@name"/>
			<xsl:text>.</xsl:text>
			<xsl:value-of select="@name"/>
			<xsl:text>(</xsl:text>
			<xsl:apply-templates select="cm:param">
				<xsl:with-param name="call" select="$call"/>
			</xsl:apply-templates>
			<xsl:text>)</xsl:text>
			<xsl:if test="cm:return">
				<xsl:text> => </xsl:text>
				<xsl:apply-templates select="cm:return">
					<xsl:with-param name="call" select="$call"/>
				</xsl:apply-templates>
			</xsl:if>
		</a>
		<xsl:text> </xsl:text>
		<span style="font-size: 80%">
			<xsl:variable name="sorcerer_signature" 
				select="string-join(for $p in $_pts return 'undefined', ',')"/>
			<a href="{$srcDocLink/@url}{$_uri}.{$srcDocLink/@ext}#{@name}({$sorcerer_signature})"
				title="Show source code of {@package}.{@type}">
				<img src="{$toRootPath}/livcos.org/web/img/external.png"/>
			</a>
		</span>
	</xsl:template>
	
	<xsl:template match="cm:param">
		<xsl:param name="call"/>
		<xsl:variable name="id" select="f:entity-id(.)"/>
		<xsl:variable name="val" select="$call/code:value[@ref = $id]"/>
		<xsl:variable name="valId">
			<xsl:choose>
				<xsl:when test="$val">
					<xsl:value-of select="$val/@id"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>/livcos.org/type/template/livcos.org/ns/dev/code/value#template_1:</xsl:text>
					<xsl:value-of select="$id"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:if test="position() > 1">, </xsl:if>
		<span id="{$valId}">
			<xsl:attribute name="title">
				<xsl:apply-templates select="cm:type"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="@name"/>
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="$val">
					<xsl:apply-templates select="$val/code:val" mode="value">
						<xsl:with-param name="type" select="cm:type"/>
					</xsl:apply-templates>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="tokenize(cm:type/@ref-name, '\.')[last()]"/>
					<xsl:text> </xsl:text>
					<xsl:value-of select="@name"/>
				</xsl:otherwise>
			</xsl:choose>
		</span>
	</xsl:template>
	
	<xsl:template match="cm:return">
		<xsl:param name="call"/>
		<span>
			<xsl:attribute name="title">
				<xsl:apply-templates select="cm:type"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="@name"/>
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="$call/code:return">
					<xsl:apply-templates select="$call/code:return" mode="value">
						<xsl:with-param name="type" select="cm:type"/>
					</xsl:apply-templates>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="tokenize(cm:type/@ref-name, '\.')[last()]"/>
				</xsl:otherwise>
			</xsl:choose>
		</span>
	</xsl:template>
	
	<xsl:template match="code:return">
		<xsl:param name="type"/>
		<div class="src_loc_title">
			<xsl:apply-templates select="." mode="value">
				<xsl:with-param name="type" select="$type"/>
			</xsl:apply-templates>
		</div>
	</xsl:template>
	
	<xsl:template match="code:val | code:return" mode="value">
		<xsl:param name="type"/>
		<xsl:choose>
			<xsl:when test="$type/@ref-name = 'java.lang.String'">
				<xsl:text>"</xsl:text>
				<xsl:value-of select="."/>
				<xsl:text>"</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="cm:type">
		<xsl:value-of select="@ref-name"/>
		<xsl:if test="cm:param">
			<xsl:text>&lt;</xsl:text>
			<xsl:for-each select="cm:param">
				<xsl:if test="position() > 1">, </xsl:if>
				<xsl:apply-templates select="cm:type"/>
			</xsl:for-each>
			<xsl:text>&gt;</xsl:text>
		</xsl:if>
	</xsl:template>
	
</xsl:stylesheet>
